package com.example.qxfw.shfx.Controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.shfx.Service.MylfxService;
import com.example.qxfw.shfx.dto.Mylfx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiIgnore
@Api(tags = "山洪防汛--面雨量分析")
@CrossOrigin
@RestController
@RequestMapping("/AreaRainfallAnalysisController")
public class AreaRainfallAnalysisController {
    @Autowired
    private MylfxService mylfxService;

    @ApiOperation("山洪防汛--面雨量分析-实时面雨量")
    @GetMapping("/realTimeSurfaceRainfall")
    public SysResult realTimeSurfaceRainfall(@Param("startDate") String startDate,@Param("endDate") String endDate,@Param("startTime") String startTime,@Param("endTime") String endTime,@Param("areaType") Short areaType
    ){
        List<Mylfx> list = mylfxService.RealTimeSurfaceRainfall(startDate, endDate, startTime, endTime, areaType);
        if (list.size()>0) {

            Map<String, Map<String, Map<String, Double>>> rainSumByAreaAndLocation = new HashMap<>();

            for (Mylfx mylfx : list) {
                String areaName = mylfx.getAreaName();
                String lat = String.valueOf(mylfx.getLat());
                String lon = String.valueOf(mylfx.getLon());
                double rain = mylfx.getRain();

                // 获取或创建对应的地区Map
                Map<String, Map<String, Double>> areaMap = rainSumByAreaAndLocation.computeIfAbsent(areaName, k -> new HashMap<>());

                // 获取或创建对应的纬度Map
                Map<String, Double> latMap = areaMap.computeIfAbsent(lat, k -> new HashMap<>());

                // 累积雨量
                latMap.put(lon, latMap.getOrDefault(lon, 0.0) + rain);
            }

            // 重新组装为List<Mylfx>
            List<Mylfx> resultMylfxList = new ArrayList<>();
            for (Map.Entry<String, Map<String, Map<String, Double>>> areaEntry : rainSumByAreaAndLocation.entrySet()) {
                String areaName = areaEntry.getKey();
                Map<String, Map<String, Double>> areaMap = areaEntry.getValue();
                for (Map.Entry<String, Map<String, Double>> latEntry : areaMap.entrySet()) {
                    String lat = latEntry.getKey();
                    Map<String, Double> lonMap = latEntry.getValue();
                    for (Map.Entry<String, Double> lonEntry : lonMap.entrySet()) {
                        String lon = lonEntry.getKey();
                        double rainSum = lonEntry.getValue();

                        //保留一位小数
                        String  rainSumStr = String.format("%.1f",rainSum);

                        // 创建新的Mylfx对象
                        Mylfx newMylfx = new Mylfx(areaName, Double.parseDouble(rainSumStr), Double.parseDouble(lon), Double.parseDouble(lat));

                        // 添加到结果集合
                        resultMylfxList.add(newMylfx);
                    }
                }
            }
            return SysResult.success(resultMylfxList);
        }
        return SysResult.fail("无符合条件的数据！！！");
    }

    @ApiOperation("山洪防汛--面雨量分析-预报面雨量")
    @GetMapping("/forecastedSurfaceRainfall")
    public SysResult forecastedSurfaceRainfall(String date, String time, short ntimes, short areatype){
        List<Mylfx> list = mylfxService.forecastedSurfaceRainfall(date, time, ntimes, areatype);
        if (list.size()>0) {
            return SysResult.success(list);
        }
        return SysResult.fail("无符合条件的数据！！！");
    }

}
